//
//  KKTableViewController.m
//  KKListViewDemo
//
//  Created by liaozhenming on 16/11/4.
//  Copyright © 2016年 liaozhenming. All rights reserved.
//

#import "KKTableViewController.h"

#import "UIViewController+List.h"

#import "TestTableViewCell.h"
#import "TestTableViewHeader.h"
#import "TestTableViewFooter.h"

#import "UIScrollView+KKRefresh.h"

@interface KKTableViewController ()

@property (nonatomic, strong) NSMutableArray *arr_data;

@property (nonatomic, assign) NSInteger dataCount;

@end

@implementation KKTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.automaticallyAdjustsScrollViewInsets = YES;
    
//    [self kk_registerTableView: self.tableView delegate: nil];
    [self.tableView registerNib: [UINib nibWithNibName: @"TestTableViewCell" bundle: nil] forCellReuseIdentifier: @"TestTableViewCell"];
//    [self.tableView registerClass: [TestTableViewHeader class] forHeaderFooterViewReuseIdentifier: @"TestTableViewHeader"];
//    [self.tableView registerClass: [TestTableViewFooter class] forHeaderFooterViewReuseIdentifier: @"TestTableViewFooter"];
//    
//    [self private_initData];
    
    self.tableView.kk_header = [KKRefreshNormalHeader headerWithRefreshingBlock:^{
        NSLog(@"kk_header");
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            self.dataCount=16;
            [self.tableView.kk_header kk_endRefreshing];
            self.tableView.kk_footer.state = KKRefreshStateNormal;
        });
    }];
    
    self.dataCount = 16;
    
    self.tableView.kk_footer = [KKRefreshNormalFooter footerWithRefreshingBlock:^{
        NSLog(@"kk_footer");
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            self.dataCount+=10;
            [self.tableView.kk_footer kk_endRefreshing];
            self.tableView.kk_footer.state = KKRefreshStateNoMoreData;
        });
    }];
    
    [self.tableView.kk_header kk_beginRefreshing];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 

- (void)private_initData {
    
    _arr_data = [NSMutableArray array];
    
    {
        KKListViewCellItem * cellItem1 = [KKListViewCellItem kk_cellItemWithKey: @"" rowHeight:30.0 cellClassName: NSStringFromClass([TestTableViewCell class])];
        KKListViewCellItem * cellItem2 = [KKListViewCellItem kk_cellItemWithKey: @"" rowHeight:50.0 cellClassName: NSStringFromClass([TestTableViewCell class])];
        KKListViewCellItem * cellItem3 = [KKListViewCellItem kk_cellItemWithKey: @"" rowHeight:70.0 cellClassName: NSStringFromClass([TestTableViewCell class])];
        
        KKListViewSectionItem *sectionItem = [KKListViewSectionItem kk_sectionItemWithKey: @"1" cellItems: @[cellItem1, cellItem2, cellItem3]];
        sectionItem.sectionHeaderSize = CGSizeMake(320, 120);
        sectionItem.sectionHeaderClassName = NSStringFromClass([TestTableViewHeader class]);
        sectionItem.sectionFooterSize = CGSizeMake(320, 80);
        sectionItem.sectionFooterClassName = NSStringFromClass([TestTableViewFooter class]);
        [_arr_data addObject: sectionItem];
    }
    
    {
        KKListViewCellItem * cellItem1 = [KKListViewCellItem kk_cellItemWithKey: @"" rowHeight:40 cellClassName: NSStringFromClass([TestTableViewCell class])];
        KKListViewCellItem * cellItem2 = [KKListViewCellItem kk_cellItemWithKey: @"" rowHeight:60.0 cellClassName: NSStringFromClass([TestTableViewCell class])];
        KKListViewCellItem * cellItem3 = [KKListViewCellItem kk_cellItemWithKey: @"" rowHeight:80.0 cellClassName: NSStringFromClass([TestTableViewCell class])];
        
        KKListViewSectionItem *sectionItem = [KKListViewSectionItem kk_sectionItemWithKey: @"1" cellItems: @[cellItem1, cellItem2, cellItem3]];
        [_arr_data addObject: sectionItem];
    }
    {
        KKListViewCellItem * cellItem1 = [KKListViewCellItem kk_cellItemWithKey: @"" rowHeight:40 cellClassName: NSStringFromClass([TestTableViewCell class])];
        KKListViewCellItem * cellItem2 = [KKListViewCellItem kk_cellItemWithKey: @"" rowHeight:60.0 cellClassName: NSStringFromClass([TestTableViewCell class])];
        KKListViewCellItem * cellItem3 = [KKListViewCellItem kk_cellItemWithKey: @"" rowHeight:80.0 cellClassName: NSStringFromClass([TestTableViewCell class])];
        
        KKListViewSectionItem *sectionItem = [KKListViewSectionItem kk_sectionItemWithKey: @"1" cellItems: @[cellItem1, cellItem2, cellItem3]];
        [_arr_data addObject: sectionItem];
    }
    
    [self setKk_sectionItems: _arr_data];
}

#pragma mark - 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataCount;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TestTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: @"TestTableViewCell" forIndexPath: indexPath];
    cell.textLabel.text = [NSString stringWithFormat: @"%@",@(indexPath.row + 1)];
    return cell;
}

@end
